/*
 * Created on 2006-4-21 12:24:08
 */
package cn.com.sbell.vss.csg.vsclient.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

import cn.com.sbell.vss.csg.vap.bean.ImageParameterInfos.ImageParameterInfo;

/**
 * @author liang.a.chen
 */
public class PropertiesHolder {

	private static final String keyValueSeparators = "=:";

	private static final String whiteSpaceChars = " \t\r\n\f";

	// private static final char commentChar = '#';

	private HttpServletRequest request;
	private Map map;

	public PropertiesHolder() {
		map = new HashMap();
	}
	
	private PropertiesHolder(Map map) {
		this.map = map;
	}
	
	private PropertiesHolder(HttpServletRequest request) {
		this.request=request;//保存request用于获取sessionID
		this.map = request.getParameterMap();
	}

	public static PropertiesHolder getInstanceFromRequest(
			HttpServletRequest request) {
		return new PropertiesHolder(request);
	}

	public Object populate(Object object) {
		try {
			BeanUtils.populate(object, map);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return object;
	}

	public Integer getIntProperty(String key) {
		return this.getIntProperty(key, 0);
	}

	public Integer[] getIntProperties(String key) {
		String[] str = this.getStrProperties(key);
		Integer[] values = new Integer[str.length];
		for (int i = 0; i < values.length; i++) {
			values[i] = null;
			try {
				values[i] = new Integer(str[i]);
			} catch (Exception e) {
				continue;
			}
		}
		return values;
	}

	public Integer getIntProperty(String key, int index) {
		Integer value = null;
		try {
			value = new Integer(getStrProperty(key, index));
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return value;
	}

	public Boolean getBoolProperty(String key, int index) {
		Boolean value = Boolean.FALSE;
		try {
			value = new Boolean(getStrProperty(key, index));
		} catch (Exception e) {
		}
		return value;
	}

	public Boolean getBoolProperty(String key) {
		return getBoolProperty(key, 0);
	}

	public Boolean[] getBoolProperties(String key) {
		String[] str = this.getStrProperties(key);
		Boolean[] values = new Boolean[str.length];
		for (int i = 0; i < values.length; i++) {
			values[i] = Boolean.FALSE;
			try {
				values[i] = new Boolean(str[i]);
			} catch (Exception e) {
				continue;
			}
		}
		return values;
	}

	public String getStrProperty(String key) {
		return this.getStrProperty(key, 0);
	}

	public String getStrProperty(String key, int index) {
		String val = null;
		String[] values = this.getStrProperties(key);
		if (values != null) {
			val = values[index];
		}
		return val;
	}

	public String[] getStrProperties(String key) {
		return (String[]) map.get(key);
	}

	public void load(Reader reader) {

		BufferedReader in = new BufferedReader(reader);
		String line;
		while (true) {
			// Get next line
			try {
				line = in.readLine();
			} catch (IOException ioe) {
				return;
			}
			if (line == null)
				return;
			if (line.length() > 0) {
				// Skip the whitespace before key
				int keyStart = 0;
				int valEnd = line.length() - 1;
				for (; keyStart <= valEnd; keyStart++) {
					if (whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1)
						break;
				}

				// Skip the whitespace after value
				for (; valEnd >= keyStart; valEnd--) {
					if (whiteSpaceChars.indexOf(line.charAt(valEnd)) == -1)
						break;
				}
				// Blank lines are ignored
				if (valEnd < keyStart)
					continue;
				// comment lines (start with '#') are ignored
				if (line.charAt(keyStart) == '#')
					continue;
				// Find separation between key and value
				int separatorIndex;
				for (separatorIndex = keyStart; separatorIndex <= valEnd; separatorIndex++) {
					if (keyValueSeparators.indexOf(line.charAt(separatorIndex)) != -1)
						break;
				}
				// Ignored if no Separators or start with
				if (keyStart == separatorIndex || separatorIndex > valEnd)
					continue;
				// Skip over whitespace after key
				int keyEnd;
				for (keyEnd = separatorIndex - 1; keyEnd >= keyStart; keyEnd--) {
					if (whiteSpaceChars.indexOf(line.charAt(keyEnd)) == -1)
						break;
				}
				// Skip over whitespace before value
				int valStart;
				for (valStart = separatorIndex + 1; valStart <= valEnd; valStart++) {
					if (whiteSpaceChars.indexOf(line.charAt(valStart)) == -1)
						break;
				}
				String key = line.substring(keyStart, keyEnd + 1);
				String value = line.substring(valStart, valEnd + 1);
				String[] values = (String[]) map.get(key);
				String[] temp = null;
				if (values == null) {
					temp = new String[] { value };
				} else {
					temp = new String[values.length + 1];
					int i = 0;
					for (; i < values.length; i++) {
						temp[i] = values[i];
					}
					temp[i] = value;

				}
				map.put(key, temp);
			}
		}
	}

	public void load(String source) {
		load(new StringReader(source));
	}

	public void load(InputStream inStream) {
		load(new InputStreamReader(inStream));
	}

	public static void main(String[] args) {
		Map map = new HashMap();
		map.put("iFrameInterval", new String[] { "100" });
		map.put("frameRate", new String[] { "111" });
		PropertiesHolder holder = new PropertiesHolder(map);
		ImageParameterInfo imgInfo = (ImageParameterInfo) holder
			.populate(new ImageParameterInfo());
		System.out.println("haha:" + imgInfo.getFrameRate());
	}

	/**
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * @param request the request to set
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
}
